package com.supersit.greensproject.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.supersit.greensproject.R;
import com.supersit.greensproject.adapter.SettingDataAdapter;
import com.supersit.greensproject.resources.Resources;
import com.supersit.greensproject.utils.AppManager;
import com.supersit.greensproject.view.MyTitleBar;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;

/**
 * Created by 刘威 on 2015/11/9.
 */
@ContentView(R.layout.activity_setting)
public class SettingActivity extends BaseActivity {
    private Context context;
    private MyTitleBar myTitleBar;
    @ViewInject(R.id.lv_setting_menu)
    private ListView lvSettingMenu;
    @ViewInject(R.id.btn_log_off)
    private Button btnLogOff;
    private SettingDataAdapter adapter;
    private ArrayList<String> settingData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        context = this;
        setTitleBar();
        setListViewData();
        bindListener();

    }

    private void setTitleBar() {
        myTitleBar = new MyTitleBar(context, findViewById(R.id.setting_title));
        myTitleBar.setTitleText(getResources().getString(R.string.setting));
        myTitleBar.setChildVisibility(false, myTitleBar.getmImgRight());
    }

    private void bindListener() {
        myTitleBar.setImg1Listener(onClickListener);
        lvSettingMenu.setOnItemClickListener(onItemClickListener);
        btnLogOff.setOnClickListener(onClickListener);
    }

    private void setListViewData() {
        settingData = new ArrayList<String>();
        for (int i = 0; i < Resources.settingMenu.length; i++) {
            settingData.add(Resources.settingMenu[i]);
        }
        adapter = new SettingDataAdapter(context, settingData);
        lvSettingMenu.setAdapter(adapter);
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.title_bar_left:
                    finish();
                    break;
                case R.id.btn_log_off:
                    startActivity(new Intent(context, LoginActivity.class));
                    AppManager.getAppManager().finishAllActivity();
                    finish();
                    break;
            }
        }
    };
    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch (position) {
                case 0:
                    startActivity(new Intent(context, UpdatePasswordActivity.class));
                    break;
                case 1:
                    startActivity(new Intent(context, VersionInfoActivity.class));
                    break;
            }
        }
    };
}
